home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / gui / js_tools123.lha / js_tools / demos / Demo_3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-27  |  16.5 KB  |  451 lines

  1. #include <exec/types.h>
  2. #include <proto/exec.h>
  3. #include <exec/memory.h>
  4. #include <proto/js_tools.h>
  5. #include <js_tools.h>
  6. #include <proto/intuition.h>
  7. #include <intuition/intuition.h>
  8. #include <proto/gadtools.h>
  9. #include <libraries/gadtools.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <stddef.h>
  13.  
  14. /*
  15.  * This is a short demo of the js_tools.library lvExtraWindow gadget.
  16.  * It opens a window on workbench and draws some listviews and opens
  17.  * a lvExtraWindow
  18.  * This in no realy good code, its a DEMO CODE!
  19.  *
  20.  * js_tools.library is copyright (c) by J.Schmitz in 1995
  21.  * and may be free copied and used as freeware
  22.  *
  23.  * This Demo is copyright (c) by J.Schmitz in 1995
  24.  * and may be free copied and used with the js_tools.library distribution.
  25.  * Parts of the code may be used in own programs.
  26.  * Military use of js_tools.library or this demo if not allowed!
  27.  *
  28.  * First parts are only a copy of Demo_2.
  29.  *
  30.  */
  31.  
  32. #define lvwidth     260
  33. #define lvheight    80
  34.  
  35. struct Library  *JS_ToolsBase;
  36. struct Library  *GadtoolsBase;
  37. struct IntuitionBase *IntuitionBase;
  38.  
  39. /* hm, a contant variable.... */
  40. /* I use topaz 8 because it's easier to do in a demo! */
  41. struct TextAttr ta={"topaz.font",8,0,0};
  42.  
  43.  
  44. /* here a struct for our texts */
  45. struct tnode {
  46.     struct Node n;
  47.     char        txt[60];
  48. };
  49.  
  50. /* here a struct for our multi column texts */
  51. struct tcnode {
  52.     struct Node n;
  53.     char        txt1[20];
  54.     char        txt2[20];
  55.     char        txt3[10];
  56. };
  57.  
  58. void FreeList(struct List *ls)
  59. {
  60.     struct Node *n;
  61.  
  62.     while (n=RemTail(ls))
  63.     {
  64.         FreeVec(n);
  65.     }
  66. }
  67.  
  68. void genText(struct List *ls,int nr)
  69. {
  70.     struct tnode *n;
  71.     int i;
  72.  
  73.     for (i=0;i<100;i++)
  74.     {
  75.         if (n=AllocVec(sizeof(struct tnode),MEMF_CLEAR))
  76.         {
  77.             sprintf(n->txt,"Line %d - LV %d",i,nr);
  78.             n->n.ln_Name=n->txt;
  79.             AddTail(ls,(struct Node*)n);
  80.         }
  81.     }
  82. }
  83.  
  84. struct Gadget *gen1(struct Gadget *gad,struct Window *win,APTR vi,char *txt)
  85. {
  86.     /* A listview for our window: READONLY with FormatText freature */
  87.     /* This listview should have key "r" as keyboard shortcut. */
  88.     /* But currently js_tools listview doesn't support GadgetText, */
  89.     /* so we make a tick with a gadtools TEXT_KIND gadget. */
  90.  
  91.     struct NewGadget    ng;
  92.     struct TagItem      ti[]={
  93.         {lv_FormatText,0},
  94.         {lv_ScrollWidth,20},
  95.         {lv_ReadOnly,TRUE},
  96.         {TAG_DONE,0}
  97.         };
  98.     struct TagItem      ti2[]={
  99.         {GTTX_Border,FALSE},
  100.         {GT_Underscore,'_'},
  101.         {TAG_DONE,0}
  102.         };
  103.  
  104.     ti[0].ti_Data=(ULONG)txt;
  105.  
  106.     ng.ng_LeftEdge      =win->BorderLeft+5;
  107.     ng.ng_TopEdge       =win->TopEdge+3+12;
  108.     ng.ng_Width         =lvwidth;
  109.     ng.ng_Height        =0;
  110.     ng.ng_GadgetText    ="_readonly";
  111.     ng.ng_TextAttr      =&ta;
  112.     ng.ng_GadgetID      =0;
  113.     ng.ng_Flags         =PLACETEXT_ABOVE;
  114.     ng.ng_VisualInfo    =vi;
  115.     ng.ng_UserData      =NULL;
  116.     gad=CreateGadgetA(TEXT_KIND,gad,&ng,&ti2[0]);
  117.  
  118.     ng.ng_LeftEdge      =win->BorderLeft+5;
  119.     ng.ng_TopEdge       =win->TopEdge+3+12;
  120.     ng.ng_Width         =lvwidth;
  121.     ng.ng_Height        =lvheight;
  122.     ng.ng_GadgetText    =NULL; /* Currently not supported. Set to NULL! */
  123.     ng.ng_TextAttr      =&ta;
  124.     ng.ng_GadgetID      =1;
  125.     ng.ng_Flags         =0;
  126.     ng.ng_VisualInfo    =vi;
  127.     ng.ng_UserData      =NULL;
  128.     return LV_CreateListViewA(LISTVIEW1_KIND,gad,&ng,&ti[0]);
  129. }
  130. struct Gadget *genLX(struct Window *win,APTR vi,struct List *ls)
  131. {
  132.     /* this is our lvExtraWindow gadget! */
  133.     /* it has mark mode active */
  134.  
  135.     struct lvExtraWindow    lvew;
  136.     struct TagItem      ti[]={
  137.         {lv_Labels,0},
  138.         {lv_ScrollWidth,20},
  139.         {lv_ShowSelected,0},
  140.         {lv_MarkOn,TRUE},
  141.         {TAG_DONE,0}
  142.         };
  143.  
  144.     ti[0].ti_Data=(ULONG)ls;
  145.     genText(ls,2);
  146.  
  147.     lvew.lvx_win    =win;
  148.     lvew.lvx_vi     =vi;
  149.     lvew.lvx_TextAttr   =&ta;
  150.     lvew.lvx_LeftEdge   =win->LeftEdge+win->Width;
  151.     lvew.lvx_TopEdge    =win->TopEdge+win->BorderTop;
  152.     lvew.lvx_Width      =200;
  153.     lvew.lvx_Height     =100;
  154.     lvew.lvx_MaxWidth   =0;             /* no limit */
  155.     lvew.lvx_MaxHeight  =0;
  156.     lvew.lvx_GadgetID   =2;
  157.     lvew.lvx_UserData   =NULL;
  158.     lvew.lvx_Title      ="lvExtraWindow Title";
  159.     lvew.lvx_Flags      =LVXF_RAWKEY | LVXF_CLOSEGADGET | LVXF_DRAGGADGET | LVXF_SIZEGADGET;
  160.                 /* we want IDCMP_RAWKEY and a CLOSEGADGET with IDCMP_CLOSEWINDOW */
  161.  
  162.     return LV_CreateExtraListViewA(&lvew,&ti[0]);
  163. }
  164.  
  165. struct Gadget *genB(struct Gadget *gad,struct Window *win,APTR vi,char *txt,int x,int width,int nr)
  166. {
  167.     /* make a button */
  168.  
  169.     struct NewGadget    ng;
  170.  
  171.     ng.ng_LeftEdge      =win->BorderLeft+5+x;
  172.     ng.ng_TopEdge       =win->TopEdge+3+lvheight+4+12;
  173.     ng.ng_Width         =width;
  174.     ng.ng_Height        =12;
  175.     ng.ng_GadgetText    =txt;
  176.     ng.ng_TextAttr      =&ta;
  177.     ng.ng_GadgetID      =nr;
  178.     ng.ng_Flags         =PLACETEXT_IN;
  179.     ng.ng_VisualInfo    =vi;
  180.     ng.ng_UserData      =NULL;
  181.     return CreateGadgetA(BUTTON_KIND,gad,&ng,NULL);
  182. }
  183. struct Gadget *genT(struct Gadget *gad,struct Window *win,APTR vi,char *txt,int x,int width)
  184. {
  185.     /* make a button */
  186.  
  187.     struct NewGadget    ng;
  188.     struct TagItem      ti[]={
  189.         {GTTX_Text,0},
  190.         {GTTX_Border,TRUE},
  191.         {TAG_DONE,0}
  192.         };
  193.  
  194.     ti[0].ti_Data=(ULONG)txt;
  195.  
  196.     ng.ng_LeftEdge      =win->BorderLeft+5+x;
  197.     ng.ng_TopEdge       =win->TopEdge+3+lvheight+4+12;
  198.     ng.ng_Width         =width;
  199.     ng.ng_Height        =12;
  200.     ng.ng_GadgetText    =NULL;
  201.     ng.ng_TextAttr      =&ta;
  202.     ng.ng_GadgetID      =0;
  203.     ng.ng_Flags         =PLACETEXT_LEFT;
  204.     ng.ng_VisualInfo    =vi;
  205.     ng.ng_UserData      =NULL;
  206.     return CreateGadgetA(TEXT_KIND,gad,&ng,&ti[0]);
  207. }
  208. void textOut(struct Gadget *gad,struct Window *win,char *txt)
  209. {
  210.     struct TagItem ti[]={
  211.         {GTTX_Text,0},
  212.         {TAG_DONE,0},
  213.         };
  214.     ti[0].ti_Data=(ULONG)txt;
  215.     GT_SetGadgetAttrsA(gad,win,NULL,&ti[0]);
  216. }
  217.  
  218. void TheDemo(void)
  219. {
  220.     /* first, we need some (struct List) or (struct MinList) */
  221.     /* I take (struct List) in this demo because then I have so write */
  222.     /* no so much casts! */
  223.     struct List ls2;
  224.  
  225.     struct Gadget       *glp=NULL,*gad;
  226.     APTR                vi=NULL;
  227.     struct Window       *win;
  228.     struct Screen       *scr;
  229.     struct Gadget       *lv1,*lv2,*txtgg;
  230.     struct IntuiMessage *my_MSG;
  231.     BOOL                ende=FALSE;
  232.     char                buffer[60];
  233.     char                gg1txt[]=   /* the text for our readonly listview */
  234.        "This is a Demo of the lvExtraWindow and the FormatText function.\n\nClick and feel!\n\\
  235.        The text here is formated automatically for this listview.\nYou may move up und down with\\
  236.        R and SHIFT R. ALT R and SHIFT ALT R moves to top or bottom.\n\n\n\\
  237.        The lvExtraWindow listview can be handled with the cursor keys and SPACE.\n\n\\
  238.        Try it....";
  239.  
  240.     NewList(&ls2);
  241.  
  242.     /* well, because we have no own screen and I don't have the time */
  243.     /* to use this PublicScreen things, I open the window and read its */
  244.     /* WScreen. */
  245.  
  246.     if (win=OpenWindowTags(NULL,WA_Activate,TRUE,
  247.                                 WA_CloseGadget,TRUE,
  248.                                 WA_DepthGadget,TRUE,
  249.                                 WA_DragBar,TRUE,
  250.                                 WA_SmartRefresh,TRUE, /* it's a Demo, sorry :-) */
  251.                                 WA_IDCMP,LISTVIEWIDCMP|IDCMP_CLOSEWINDOW|IDCMP_RAWKEY,
  252.                                 WA_InnerWidth,lvwidth+5+5,
  253.                                 WA_InnerHeight,lvheight+3+3+12+4+12,
  254.                                 WA_Title,"js_tools Listview Demo",
  255.                                 TAG_DONE))
  256.     {
  257.         /* ah, our window is open! */
  258.         scr=win->WScreen;
  259.         if (vi=GetVisualInfoA(scr,NULL))
  260.         {
  261.             gad=CreateContext(&glp);
  262.             if (lv1=gen1(gad,win,vi,gg1txt))
  263.             {
  264.                 gad=lv1;
  265.             }
  266.             lv2=genLX(win,vi,&ls2);
  267.               /* this is no gadget of our gadget list */
  268.               /* so we MUST NOT apply it to this! */
  269.  
  270.             /* and finally we make QUIT gadtools button */
  271.             gad=genB(gad,win,vi,"Quit",0,100,10);
  272.             gad=genT(gad,win,vi,"by J.Schmitz",105,lvwidth+5+5-110);
  273.             txtgg=gad;
  274.  
  275.             AddGList(win,glp,~0,~0,NULL);
  276.             RefreshGList(glp,win,NULL,-1);
  277.             LV_RefreshWindow(win,NULL);
  278.  
  279.             do
  280.             {
  281.                 my_MSG=(struct IntuiMessage*)WaitPort(win->UserPort);
  282.                 my_MSG=LV_GetIMsg(win->UserPort);
  283.                 if (my_MSG)
  284.                 {
  285.                     switch (my_MSG->Class)
  286.                     {
  287.                         case IDCMP_CLOSEWINDOW:
  288.                             /* well, CLOSEWINDOW could also be send from the lvExtraWindow! */
  289.                             /* so we have to check this! */
  290.                             if (my_MSG->IDCMPWindow==win)
  291.                             {
  292.                                 /* this is our window */
  293.                                 ende=TRUE;
  294.                             }
  295.                             else
  296.                             {
  297.                                 /* aha, from a lvExtraWindow */
  298.                                 /* to see which of them (well, we have only one, */
  299.                                 /* but if we had more....) */
  300.                                 if (my_MSG->IAddress==lv2)
  301.                                 {
  302.                                     textOut(txtgg,win,"Close lvExtraWindow.");
  303.  
  304.                                     /* OK, this is from our lvExtraWindow 1 (if we had more) */
  305.                                     /* The user told use to close it, so we close it */
  306.                                     /* BUT BEFORE WE MUST REPLY THE MESSAGE WE GOT!!! */
  307.                                     /* (or intuiton will crash. */
  308.                                                                         LV_ReplyIMsg(my_MSG);
  309.                                     LV_FreeListView(lv2);
  310.  
  311.                                     /* to remember that is has been close we set the pointer to NULL */
  312.                                     /* you MUST NOT use this pointer again - the window is closed and gone! */
  313.                                     lv2=NULL;
  314.                                     /* and the same trick for the IntuiMessage */
  315.                                     /* as it is save to give LV_ReplyIMsg() a NULL pointer there no problem! */
  316.                                     my_MSG=NULL;
  317.                                 }
  318.                             }
  319.                             break;
  320.                         case IDCMP_GADGETUP:
  321.                         case IDCMP_GADGETDOWN:
  322.                             /*
  323.                              * the user selected something
  324.                              * we can find the number in my_MSG->Code
  325.                              * and the gadget in my_MSG->IAddress
  326.                              *
  327.                              * we have a DEMO, so I don't write here some
  328.                              * usefull code!
  329.                              * Only a example to show you the multiselection-
  330.                              * result for lv2 - like Demo 2.
  331.                              *
  332.                              */
  333.                             gad=(struct Gadget*)my_MSG->IAddress;
  334.                             switch (gad->GadgetID)
  335.                             {
  336.                                 case 10:
  337.                                     ende=TRUE;
  338.                                     break;
  339.                                 case 1:
  340.                                     /* well, this is really impossible! */
  341.                                     /* we have a readonly listview here. */
  342.                                     break;
  343.                                 case 2:
  344.                                     sprintf(buffer,"lvExtraWindow: %d",my_MSG->Code,my_MSG->MouseY);
  345.                                     textOut(txtgg,win,buffer);
  346.  
  347.                                     if (my_MSG->Qualifier & MARK_QUALIFIER_SET)
  348.                                     {
  349.                                         /* aha, user made multiselection */
  350.                                         /* in a real program we have to look in */
  351.                                         /* my_MSG->MouseX for the top line of the marked block */
  352.                                         /* and in my_MSG->MouseY for the bottom line. */
  353.                                         /* Well, this is a DEMO, and you know... ;-) */
  354.  
  355.                                         sprintf(buffer,"Multi ON: %d - %d (Sel: %d)",my_MSG->MouseX,my_MSG->MouseY,my_MSG->Code);
  356.                                         textOut(txtgg,win,buffer);
  357.                                     }
  358.                                     if (my_MSG->Qualifier & MARK_QUALIFIER_CLEAR)
  359.                                     {
  360.                                         /* aha, user made multiOFFselection */
  361.                                         /* in a real program we have to look in */
  362.                                         /* my_MSG->MouseX for the top line of the marked block */
  363.                                         /* and in my_MSG->MouseY for the bottom line. */
  364.                                         /* Only a DEMO. */
  365.  
  366.                                         sprintf(buffer,"Multi OFF: %d - %d (Sel: %d)",my_MSG->MouseX,my_MSG->MouseY,my_MSG->Code);
  367.                                         textOut(txtgg,win,buffer);
  368.                                     }
  369.                                     /* this lvExtraWindow is handles the same way as any other listview */
  370.                                     break;
  371.                             }
  372.                             break;
  373.                         case IDCMP_RAWKEY:
  374.                             {
  375.                                 struct TagItem ti[2];
  376.                                 ULONG sel;
  377.                                 int key;
  378.                                 ti[0].ti_Tag =lv_Selected;
  379.                                 ti[0].ti_Data=(ULONG)&sel;
  380.                                 ti[1].ti_Tag =TAG_DONE;
  381.                                 if ((key=LV_KeyHandler(lv2,my_MSG,0,&ti[0]))<0)
  382.                                 {
  383.                                     /* rawkey is used for listview - here lvExtraWindow */
  384.                                     /* same like Demo 2 ! */
  385.  
  386.                                     sprintf(buffer,"lvExtraWindow: %d",sel);
  387.                                     textOut(txtgg,win,buffer);
  388.                                 }
  389.                                 else
  390.                                 {
  391.                                     /* OK, once again with the "r" key for our readonly listview */
  392.                                     /* Readonly need no taglist here, so we give it a NULL */
  393.                                     if ((key=LV_KeyHandler(lv1,my_MSG,'r',NULL))>=0)
  394.                                     {
  395.                                         /* key contains the ASCII value of the pressed key */
  396.                                         /* or 0 if no ASCII value is available. */
  397.  
  398.                                         sprintf(buffer,"Key: %c (#%d)",key,key);
  399.                                         textOut(txtgg,win,buffer);
  400.                                         if (key==27)
  401.                                         {
  402.                                             ende=TRUE;
  403.                                         }
  404.                                     }
  405.                                 }
  406.                                 /* aha, lvExtraWindows are handled as a normal part of the window. */
  407.                             }
  408.                             break;
  409.                     }
  410.                     LV_ReplyIMsg(my_MSG);
  411.                 }
  412.             }
  413.             while (!ende);
  414.  
  415.             RemoveGList(win,glp,~0);
  416.             if (lv2)
  417.             {   
  418.                 LV_FreeListView(lv2);   /* this removes and closes the lvExtraWindow */
  419.             }
  420.             LV_FreeListViews(glp);  /* this removed all listviews in this gadget list */
  421.  
  422.             FreeGadgets(glp);
  423.             FreeVisualInfo(vi);
  424.         }
  425.         CloseWindow(win);
  426.     }
  427.     FreeList(&ls2);
  428. }
  429.  
  430. void main(void)
  431. {
  432.     if (JS_ToolsBase=OpenLibrary("js_tools.library",37))
  433.     {
  434.         /* yea, we got it! */
  435.         if (GadtoolsBase=OpenLibrary("gadtools.library",37))
  436.         {
  437.             if (IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",37))
  438.             {
  439.                 /* everything is fine, now, let's start! */
  440.  
  441.                 TheDemo();
  442.  
  443.                 CloseLibrary((struct Library*)IntuitionBase);
  444.             }
  445.             CloseLibrary(GadtoolsBase);
  446.         }
  447.         CloseLibrary(JS_ToolsBase);
  448.     }
  449. }
  450.  
  451.